package com.yuandian.business.monitor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.business_monitor.po.ItSegmentDataParams;
import com.yuandian.api.business_monitor.vo.*;
import com.yuandian.api.management.entity.ConfNodeProperty;
import com.yuandian.api.management.entity.ConfSystem;
import com.yuandian.api.management.enums.GraphType;
import com.yuandian.api.management.feign.RemoteNodePropertyService;
import com.yuandian.api.management.feign.RemoteSegmentService;
import com.yuandian.api.management.feign.RemoteSystemService;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.business.monitor.mapper.SysSegmentTrendMapper;
import com.yuandian.business.monitor.service.IAllItRoadService;
import com.yuandian.business.monitor.service.IMonitorOverviewService;
import com.yuandian.enums.SegmentStatusEnum;
import com.yuandian.utils.PageDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明：全IT路径图service实现
 *
 * @Author yanjun
 * @Date 2020/9/21 9:30
 */
@Service
@Slf4j
public class AllItRoadServiceImpl implements IAllItRoadService {

	@Autowired
	private RemoteSystemService remoteSystemService;
	@Autowired
	private RemoteNodePropertyService remoteNodePropertyService;

	@Autowired
	private IMonitorOverviewService monitorOverviewService;
	@Autowired
	private SysSegmentTrendMapper sysSegmentTrendMapper;

	public static final String AllItRoadId = "8a9d39136d52c2155e68b80d167d41q7";

	/**
	 * 查询监控概况
	 *
	 * @return 监控概况对象
	 */
	@Override
	public MonitorOverviewVo queryMonitorOverview(ItSegmentDataParams params) {
		MonitorOverviewVo monitorOverviewVo = monitorOverviewService.queryRecentMonitorOverview(params);
		if (monitorOverviewVo == null) {
			// 查询无该天数据，设置默认值
			monitorOverviewVo = new MonitorOverviewVo();
			monitorOverviewVo.setSystemTotal(0);
			monitorOverviewVo.setSystemUnhealthTotal(0);
			monitorOverviewVo.setSegmentTotal(0);
			monitorOverviewVo.setSegmentUnhealthTotal(0);
			monitorOverviewVo.setBusinessTotal(0);
			monitorOverviewVo.setBusinessUnhealthTotal(0);
			monitorOverviewVo.setBusinessVisitTotal(0);
			monitorOverviewVo.setBusinessVisitUnhealthTotal(0);
			monitorOverviewVo.setUserTotal(0);
		}
		return monitorOverviewVo;
	}


	/**
	 * 查询网端详情数据
	 *
	 * @param params It路径图端查询参数对象
	 * @return 网端数据
	 */
	@Override
	public List<ItSegmentDetailVo> querySegmentData(ItSegmentDataParams params) {
		//1、查询网端详情数据
		List<SegmentDetailVo> segmentDetailVoList = sysSegmentTrendMapper.querySegmentDetail(params);
		// 2、组装vo对象
		List<ItSegmentDetailVo> list = new ArrayList<>();
		for (SegmentDetailVo segmentDetailVo : segmentDetailVoList) {
			ItSegmentDetailVo itSegmentDetailVo = new ItSegmentDetailVo();
			itSegmentDetailVo.setMapKey(segmentDetailVo.getSystemId() + "_" + segmentDetailVo.getSegmentId());
			itSegmentDetailVo.setReqCount(segmentDetailVo.getReqCount());
			itSegmentDetailVo.setAvgDealTime(segmentDetailVo.getAvgDealTime().toString() + "ms");
			Float healthRate = segmentDetailVo.getHealth();
			if (healthRate == null) {
				continue;
			}
			itSegmentDetailVo.setHealth(healthRate + "%");
			if (segmentDetailVo.getReqCount() > 0) {
				if (healthRate >= 90) {
					itSegmentDetailVo.setStatus(SegmentStatusEnum.NORMAL.getStatus());
				} else if (healthRate >= 70) {
					itSegmentDetailVo.setStatus(SegmentStatusEnum.WARNING.getStatus());
				} else {
					itSegmentDetailVo.setStatus(SegmentStatusEnum.DANGER.getStatus());
				}
			} else {
				itSegmentDetailVo.setStatus(SegmentStatusEnum.NO_DATA.getStatus());
			}
			list.add(itSegmentDetailVo);
		}
		return list;
	}


	/**
	 * 全系统IT路径图-查询网端组件详情列表
	 * @param params 请求参数
	 * @return	结果集
	 */
	@Override
	public PageDataInfo getComponentDetailsData(ItSegmentDataParams params) {
		Page<ComponentDetailsVo> page = new Page<>(params.getPageNum(), params.getPageSize());

		List<ComponentDetailsVo> restList = sysSegmentTrendMapper.getComponentDetailsData(page, params);
		return new PageDataInfo(page, restList);
	}


	/**
	 * 全系统IT路径图-查询网络详情列表
	 * @param params 请求参数
	 * @return	结果集
	 */
	@Override
	public PageDataInfo getNetworkDetailsList(ItSegmentDataParams params) {
		Page<SystemLinkPointDetailVo> page = new Page<>(params.getPageNum(), params.getPageSize());
		List<SystemLinkPointDetailVo> networkDetailsList = sysSegmentTrendMapper.getNetworkDetailsList(page, params);
		return new PageDataInfo(page, networkDetailsList);
	}

	@Override
	public R updateSegmentPosition(SegmentPositionVo segmentPositionVo) {
		ConfNodeProperty confNodeProperty = new ConfNodeProperty();
		confNodeProperty.setType(GraphType.ALL_IT_ROAD_RELATIONSHIP.getType());
		confNodeProperty.setGraphId("0");
		confNodeProperty.setNodeId(segmentPositionVo.getSegmentId().toString());
		confNodeProperty.setLocationX(Double.valueOf(segmentPositionVo.getLocationX()));
		confNodeProperty.setLocationY(Double.valueOf(segmentPositionVo.getLocationY()));
		return remoteNodePropertyService.setNodePosition(confNodeProperty);
	}

	/**
	 * 查询监控中、并且不为虚拟系统的应用系统列表
	 *
	 * @return 监控中的应用系统列表
	 */
	private List<ConfSystem> queryMonitoringAppSystem() {
		ConfSystem confSystem = new ConfSystem();
		confSystem.setStatus(1);
		confSystem.setIsVirtual(0);
		Page<ConfSystem> page = new Page();
		page = remoteSystemService.listPage(confSystem).getData();
		return page.getRecords();
	}
}
